package Presentation.GraphicUI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.TreeMap;


public class AddNewUserScreen extends MovieSystemPanel{

    private JTextField      m_username;
    private JTextField      m_firstname;
    private JTextField      m_lastname;
    private JPasswordField  m_password;
    private JComboBox       m_VIP;
    private JTextField      m_email;
    private JComboBox       m_acl;

    public AddNewUserScreen(MainScreen mainScreen){
        super(mainScreen);        

        add(createInstructionLabel("Please fill all the fields, than click the 'Submit' button"));

        m_username = createTextField("UserName: ",15);
        m_firstname = createTextField("First Name: ",15);
        m_lastname = createTextField("Last Name: ",15);
        m_password = createPasswordField("Password: ",15);
        m_VIP = createYesNoField("Is VIP user: ");
        m_email = createTextField("E-mail Address: ",15);
        LinkedList<String> aclLevels = new LinkedList<String>();
        aclLevels.add("Administrator");
        aclLevels.add("User");
        aclLevels.add("Manager");
        m_acl = createComboBox("User Access Level: ",aclLevels);

        TreeMap<Integer, JComponent> formComponents = new TreeMap<Integer, JComponent>();
        formComponents.put(1,m_username);
        formComponents.put(2,m_firstname);
        formComponents.put(3,m_lastname);
        formComponents.put(4,m_password);
        formComponents.put(5,m_email);
        formComponents.put(6,m_VIP);
        formComponents.put(7,m_acl);

        add(createFormPanel(formComponents, createSubmitButton()));
    }

    private JButton createSubmitButton(){
 		JButton button = new JButton("Submit");
        button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    try{
                        if (isLeagalValues()){
                            String VIP = "false" ;
                            if(m_VIP.getSelectedItem().toString().equals("Yes"))
                                VIP = "true";
                            m_controller.addNewUser(m_username.getText(), m_firstname.getText(), m_lastname.getText(), m_password.getText(),VIP, m_email.getText(), m_acl.getSelectedItem().toString());
                            JOptionPane.showMessageDialog(m_mainScreen,"User was added successfully");
                            m_controller.login(m_username.getText(),new String(m_password.getPassword()));
                            m_mainScreen.setActivePanel(MainScreen.MAIN_MENU);
                        }
                        else
                            JOptionPane.showMessageDialog(m_mainScreen, "please fill out ALL fileds");
                    }
                    catch(Exception ex)	{
                        JOptionPane.showMessageDialog(m_mainScreen, ex.getMessage());
                    }
            }
            });
        return button;
    }

    private boolean isLeagalValues(){
        if (m_username.getText().isEmpty()||
            m_firstname.getText().isEmpty()||
            m_lastname.getText().isEmpty()||                
            m_password.getPassword().length == 0||
            m_email.getText().isEmpty())
        return false;
        else
            return true;
    }

    public void setUp(){
         m_email.setText("");
         m_firstname.setText("");
         m_lastname.setText("");
         m_password.setText("");
         m_username.setText("");
    }
}
